package com.lx.gulimall.order.controller;

import com.lx.gulimall.order.entity.OrderEntity;
import com.lx.gulimall.order.entity.OrderItemEntity;
import com.lx.gulimall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @author lixun Email:lx3027@126.com
 * @create 2021-08-11 14:51
 */
@RestController
@Slf4j
public class RabbitSendMessage {


    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send/mq")
    public String sendMessage(@RequestParam(value = "num",defaultValue = "10") Integer num){
        OrderReturnReasonEntity order = new OrderReturnReasonEntity();
        order.setId(1L);
        order.setCreateTime(new Date());
        order.setName("张三");
        order.setSort(0);
        order.setStatus(0);
        OrderItemEntity orderItemEntity = new OrderItemEntity();
        orderItemEntity.setId(1L);
        orderItemEntity.setSkuName("有");
        // 1.发送消息，如果发送的消息是个对象，我们会使用序列化机制，将对象写出去，对象必须实现serializable
        String msg = "Hello World";
        for (int i = 0; i < num;i++){
            if(i%2==0){
                // 2. 发送的消息可以转成json
                rabbitTemplate.convertAndSend("hello-java-exchange","hello-java",order,new CorrelationData(UUID.randomUUID().toString()));
                log.info("OrderReturnReasonEntity:消息发送完成{}，第{}个消息",order,i);
            }
            rabbitTemplate.convertAndSend("hello-java-exchange","hello-java",orderItemEntity,new CorrelationData(UUID.randomUUID().toString()));
            log.info("orderItemEntity:消息发送完成{}，第{}个消息",order,i);
        }
        return "OK";
    }

}
